package com.xuxin.pda.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.pda.R;
import com.xuxin.pda.adapter.TrashLinkAdapter;
import com.xuxin.pda.base.BaseConstant;
import com.xuxin.pda.base.BasePDAActivity;
import com.xuxin.pda.bean.LinkBean;
import com.xuxin.pda.contract.TrashLinkContract;
import com.xuxin.pda.popup.ConfirmPopup;
import com.xuxin.pda.presenter.TrashLinkPresenter;
import com.xuxin.pda.utils.ACache;
import com.xuxin.pda.utils.RecycleUtils;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2021/2/3 0003
 * 箱袋关联
 */
public class TrashLinkActivity extends BasePDAActivity implements TrashLinkContract.View {
    private TrashLinkContract.Presenter presenter = new TrashLinkPresenter(this);
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.trash_link_user)
    TextView trash_link_user;
    @BindView(R.id.trash_link_box)
    TextView trash_link_box;
    @BindView(R.id.trash_link_recycle)
    RecyclerView trash_link_recycle;

    private TrashLinkAdapter mTrashLinkAdapter;
    private String userName;//用户名
    private String boxId;//箱子id
    private String packageCodes;//医废袋id

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_trash_link);
    }

    @Override
    public void initView() {
        title_name.setText("箱袋关联");
        button.setText("确认关联");
        title_right.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        userName = aCache.getAsString(BaseConstant.userName);
        trash_link_user.setText(userName);
        mTrashLinkAdapter = new TrashLinkAdapter();
        RecycleUtils.init(trash_link_recycle, 4);
        trash_link_recycle.setAdapter(mTrashLinkAdapter);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.button) {
            boxId = trash_link_box.getText().toString();
            packageCodes = StringUtils.join(mTrashLinkAdapter.getData(), ",");
            if (StringUtils.isEmpty(boxId)) {
                ToastUtils.showShort("请扫描箱子二维码");
                return;
            }
            if (StringUtils.isEmpty(packageCodes)) {
                ToastUtils.showShort("请扫码添加袋子二维码");
                return;
            }
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要关联吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            popup.setTitle("正在关联中...").show();
                            handler.sendEmptyMessageDelayed(0,500);
                        }
                    })).show();
        }
    }

    @Override
    public void onSuccess(LinkBean linkBean) {
        ToastUtils.showShort("关联成功");
        finish();
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.link(boxId, packageCodes);
            }
            return false;
        }
    });

    @Override
    public void onPDASuccess(String message) {
        if (message.startsWith("BOX")) {
            trash_link_box.setText(message);
        } else if (message.startsWith("FDW")) {
            if (!mTrashLinkAdapter.getData().contains(message)) {
                mTrashLinkAdapter.addData(message);
            } else {
                ToastUtils.showShort("请勿重复扫码");
            }
        } else {
            ToastUtils.showShort("二维码格式错误");
        }
    }
}